﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using DateBaseConnectionPool;

namespace TestMySqlConnetPoll
{
    public struct objTimeAdjust 
    {
        public int ftime;   //调整前时间，重复时间段的结束时间
        public int btime;   //调整后时间，重复时间段的开始时间
    }
    public class TimeAdjustTable
    {
        public List<objTimeAdjust> ValueList = new List<objTimeAdjust>();
        public int  ErrorFlag = 0;
        public bool ReadyValueList = false;
        private Mutex keyMutex = new Mutex();
        static private int keykey = 0;
        public void ReadValueList()
        {
            ErrorFlag = 0;
            ReadyValueList = false;
            ValueList.Clear();
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadTable));
        }

        public void InsertData(int stime, int etime)
        {
            string TableName = "TimeAdjustTable";
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    StringBuilder InsertStatement = new StringBuilder();
                    InsertStatement.Append(string.Format("insert into {0} (ftime,btime) values ({1},{2}) ", TableName, stime, etime));
                    MySqlCommand InsertCommand = new MySqlCommand(InsertStatement.ToString(), con);
                    try
                    {
                        InsertCommand.ExecuteNonQuery();
                    }
                    catch
                    { }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 6;
                    }
                }
            }
        }

        private void ThreadReadTable(object o)
        {
            string TableName = "TimeAdjustTable";
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    string SelectStatement = string.Format("select ftime,btime from {0}", TableName);
                    try
                    {
                        MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
                        selectCommand.CommandTimeout = 120;   //120s操作超时
                        MySqlDataReader read = selectCommand.ExecuteReader();
                        while (read.Read())
                        {
                            objTimeAdjust obj = new objTimeAdjust();
                            obj.ftime = read.GetInt32(read.GetOrdinal("ftime"));
                            obj.btime = read.GetInt32(read.GetOrdinal("btime"));
                            ValueList.Add(obj);
                        }
                        ReadyValueList = true;
                        read.Close();
                    }
                    catch
                    {
                        ErrorFlag = 3;
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 4;
                    }
                }
                else ErrorFlag = 6;
            }
            else ErrorFlag = 7;
        }
    }
}
